<template>
    <view class="goods-item" v-if="goods!=={}">
        <!-- 左图-->
        <view class="goods-item-left">
            <!-- 
                1. checked 勾选状态
                2. v-if 按钮状态显示
             -->
            <radio :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="radioClickHandler"></radio>
            <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
        </view>
        <!-- 右信息-->
        <view class="goods-item-right">
            <view class="goods-name">{{goods.goods_name}}</view>
            <view class="goods-info-box">
                <view class="goods-price">
                    {{goods.goods_price==undefined?'0':goods.goods_price.toFixed(2)}} 元
                </view>
                <uni-number-box :min="1" :value="goods.goods_count" v-if="showNum"
                    @change="numChangeHandler"></uni-number-box>
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        name: "my-goods",
        props: {
            // 商品对象
            goods: {
                type: Object,
                defaul: {}
            },
            // 是否展示左侧 radio 选项
            showRadio: {
                type: Boolean,
                default: false,
            },
            // 是否展示右侧 NumberBox组件
            showNum:{
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
            };
        },
        methods: {
            // 选中事件
            radioClickHandler() {
                // 回调父类方法 并 传递 goods_id和goods_state
                this.$emit('radio-change', {
                    goods_id: this.goods.goods_id,
                    goods_state: !this.goods.goods_state
                })
            },
            // numberBox 组件 数值变化时间
            numChangeHandler(value){
                // 传递 goods_id 和 goods_count 最新数量
                this.$emit('num-change',{
                    goods_id: this.goods.goods_id,
                    // 不能保证时时刻刻都是数值类型 +变量 能够快速转换数值
                    goods_count: +value
                })
            }
        }
    }
</script>

<style lang="scss">
    .goods-item {
        // 占宽
        // width: 750rpx;
        // box-sizing: border-box;
        
        display: flex;
        padding: 10px 5px;
        border-bottom: 1px solid #f0f0f0;

        .goods-item-left {
            margin-right: 5px;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .goods-pic {
                width: 100px;
                height: 100px;
                display: flex;
            }
        }

        .goods-item-right {
            display: flex;
            flex: 1;
            flex-direction: column;
            justify-content: space-between;

            .goods-name {
                font-size: 13px;
            }
            
            .goods-info-box{
                display: flex;
                align-items: center;
                justify-content: space-between;
            }

            .goods-price {
                font-size: 16xp;
                color: #d81e06;
            }
        }
    }
</style>
